//construct the main ACS analysis sample, complete with housing information and PIKs

//loop over years
forval y = 2005/2018{
	use "$data/PIK/ACS/stata/acs_xwalk_`y'", clear
	keep if pik!=""
	duplicates tag pik, gen(dup) //delete any PIKs that are mapped to multiple people in ACS
	drop if dup
	keep pik cmid pnum
	duplicates tag cmid pnum, gen(dup) //drop anybody who gets mapped to multiple PIKs
	drop if dup
	drop dup
	tempfile pik
	save `pik'

	use "$temp/acs_`y'_sample", clear
	merge m:1 cmid using "$temp/acs_`y'_sample_house", keep(match) nogen //merge on housing variables
	merge m:1 cmid pnum using `pik', keep(1 3) nogen //merge on PIKs
	gen pik_match = (pik!="")
	gen year = `y' //year variable -- don't forget!
	save "$temp/acs_`y'_matched", replace //save
}

//fix up wkw varaible
forval y = 2008/2018{
	use "$temp/acs_`y'_matched", clear
	destring wkw, replace
	save "$temp/acs_`y'_matched", replace
}

//fix up  val variable
forval y = 2005/2007{
	use "$temp/acs_`y'_matched", clear
	destring val, replace
	save "$temp/acs_`y'_matched", replace
}

//add together
clear
forval y = 2005/2018{
	append using "$temp/acs_`y'_matched"
	erase "$temp/acs_`y'_matched.dta" //kill intermediate files
}

save "$temp/acs_sample_pik", replace //save
